package cn.ixiaoxin.deepchat.service.impl;

import cn.ixiaoxin.deepchat.VO.FriendsVO;
import cn.ixiaoxin.deepchat.entity.Friend;
import cn.ixiaoxin.deepchat.mapper.FriendMapper;
import cn.ixiaoxin.deepchat.service.FriendService;
import cn.ixiaoxin.deepchat.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {

    @Autowired
    private FriendMapper friendMapper;

    @Override
    public Result<?> addFriend(Long userId, Long friendId, String remark) {
        // 检查是否已存在
        Friend existing = friendMapper.selectOne(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Friend>()
                .eq("user_id", userId)
                .eq("friend_id", friendId));
        if (existing != null) {
            return Result.error("该好友关系已存在");
        }

        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setFriendId(friendId);
        friend.setRemark(remark);
        friendMapper.insert(friend);

        return Result.success("添加成功");
    }

    @Override
    public Result<?> deleteFriend(Long userId, Long friendId) {
        friendMapper.delete(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Friend>()
                .eq("user_id", userId)
                .eq("friend_id", friendId));
        return Result.success("删除成功");
    }

    @Override
    public Result<?> getFriends(Friend friend) {
        List<FriendsVO> friends = friendMapper.getFriends(friend.getUserId());
        return Result.success(friends);
    }
}
